package cau.se.project.unittest;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.Before;
import org.junit.Test;

import cau.se.project.DSMFile;
import cau.se.project.method.CLSXControl;
import cau.se.project.method.FileControl;

public class DSMFileTest {
	private FileControl dsmFile;
	private File testDSMFile;
	
	@Before
	public void setUp() {
		dsmFile = new DSMFile();
		testDSMFile = new File("Test_DSM_File.dsm");
	}

	@Test
	public void testNewFile() {
		testDSMFile = null;
		dsmFile.newFile();

		assertNull(dsmFile.getCurrentFile());
	}

	@Test
	public void testOpenOne() throws Exception {
		assertNull(dsmFile.getCurrentFile());
		
		dsmFile.open(testDSMFile);
		
		assertThat(dsmFile.getCurrentFile(), is(testDSMFile));
	}
	
	@Test
	public void testOpenTwo() throws Exception {
		assertNull(dsmFile.getCurrentFile());
		
		assertThat(this.dsmFile.open(testDSMFile).getClass(), is(CLSXControl.class.getClass()));
	}
	
	@Test(expected=Exception.class)
	public void testOpenException() throws Exception {
		dsmFile.open(null);
	}

	@Test
	public void testSave() throws Exception {
		assertNull(dsmFile.getCurrentFile());
		CLSXControl mockCLSXControl = createMock(CLSXControl.class);
		System.out.println("test save test");
		expect(mockCLSXControl.getArray()).andReturn(new int[][] {
				{0, 1, 1},
				{1, 0, 1},
				{0, 1, 0}
		});
		expect(mockCLSXControl.getNameList()).andReturn(new String[] {"FirstRow", "SecondRow", "ThirdRow"});
		
		replay(mockCLSXControl);
		
		dsmFile.open(testDSMFile);
		dsmFile.save(mockCLSXControl);
				
		assertTrue(dsmFile.getCurrentFile().equals(testDSMFile));
	}

	/*
	@Test
	public void testSaveAs() {
		//dsmFile.saveAs(clsx, file);
		fail("Not yet implemented");
	}
	*/

	@Test
	public void testGetCurrentFile() throws Exception {
		assertNull(dsmFile.getCurrentFile());
		
		dsmFile.open(testDSMFile);
		
		assertThat(dsmFile.getCurrentFile(), is(testDSMFile));
	}
}
